package middleware

import (
	"bmember/pkg/mq"
	"context"
	"time"

	"github.com/nsqio/go-nsq"
	"go.uber.org/zap"
)

// MqStdLogger nsq 标准日志输出
func MqStdLogger(handler mq.MqHandlerFunc) mq.MqHandlerFunc {
	return func(ctx context.Context, msg *nsq.Message) error {
		start := time.Now()
		body := msg.Body
		err := handler(ctx, msg)
		du := time.Since(start)
		if len(body) > 1024 {
			body = body[:1024]
		}
		if err != nil {
			logger.For(ctx).Error("failed",
				zap.ByteString("message", body),
				zap.String("duration", du.String()),
				zap.String("error", err.Error()))
		} else {
			logger.For(ctx).Info("success",
				zap.ByteString("message", body),
				zap.String("duration", du.String()))
		}
		return err
	}
}
